package com.xiaoxiong.mami.http;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * 
 * 类的说明
 * 
 * @author zcg<br/>
 * @version 1.0<br/>
 * @email: zcg@suntektech.com<br/>
 * @datetime: 2012-12-18<br/>
 */
public class XHeader {

	/**
	 * The header for accepted encodings
	 */
	public static final String ACCEPT_ENCODING = "Accept-Encoding";

	/**
	 * The header for content encodings
	 */
	public static final String CONTENT_ENCODING = "Content-Encoding";
	
	public static final String CONTENT_LENGTH = "Content-Length";

	/**
	 * Examine the request to make sure that the browser can accept gzip
	 * encodings
	 * 
	 * @param request
	 *            the request to examine for gzip acceptance
	 * @return boolean true if the request can accept gzip
	 */
	public static boolean acceptsGzip(HttpServletRequest request) {
		return headerContains(request, ACCEPT_ENCODING, "gzip");
	}
	
	/**
	 * Examine the request to make sure that the browser can accept gzip
	 * encodings
	 * 
	 * @param request
	 *            the request to examine for gzip acceptance
	 * @return boolean true if the request can accept gzip
	 */
	public static boolean contentGzip(HttpServletRequest request) {
		return headerContains(request, CONTENT_ENCODING, "gzip");
	}

	/**
	 * Utility method to examine the given request, find the given header and
	 * see if the header contains the provided value
	 * 
	 * @param request
	 *            the request to examine
	 * @param header
	 *            the specific header to check the value on
	 * @param value
	 *            the value to look for in the header
	 * @return boolean true if the header contains the given value
	 */
	@SuppressWarnings("rawtypes")
	public static boolean headerContains(HttpServletRequest request,
			String header, String value) {
		Enumeration accepted = request.getHeaders(header);
		while (accepted.hasMoreElements()) {
			String headerValue = (String) accepted.nextElement();
			if (headerValue.indexOf(value) != -1) {
				return true;
			}
		}
		return false;
	}

}
